package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.LeaveRecord;
import com.chinacoal.hr.levelimpl.mapper.LeaveRecordMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-17 12:24
 * @description:
 **/
@Repository
public class LeaveRecordDao {
    @Resource
    private LeaveRecordMapper leaveRecordMapper;

    public IPage<LeaveRecord> getLeaveRecordList(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";
        String deptCode = condition.get("deptCode") != null ? condition.get("deptCode").toString() : "";
        String deptName = condition.get("deptName") != null ? condition.get("deptName").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String holidayType = condition.get("holidayType") != null ? condition.get("holidayType").toString() : "";
        String businessStatus = condition.get("businessStatus") != null ? condition.get("businessStatus").toString() : "";
        String startDate = condition.get("startDate") != null ? condition.get("startDate").toString() : "";
        String endDate = condition.get("endDate") != null ? condition.get("endDate").toString() : "";
        String queryRange = condition.get("queryRange") != null ? condition.get("queryRange").toString() : "";

        IPage<LeaveRecord> page = new Page(currentPage, currentPageSize);

        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORG_CODE", orgCode);
        }
        if (StringUtils.isNotBlank(holidayType)) {
            queryWrapper.eq("HOLIDAY_TYPE", holidayType);
        }
        if (StringUtils.isNotBlank(businessStatus)) {
            queryWrapper.eq("BUSINESS_STATUS", businessStatus);
        }

        // 查询范围
        if (StringUtils.isNotBlank(queryRange)) {
            if (("all").equals(queryRange)) {
            } else if (("dept").equals(queryRange)) {
                queryWrapper.eq("DEPT_CODE", deptCode);
            } else if (("person").equals(queryRange)) {
                queryWrapper.eq("EMP_CODE", empCode);
            }
        }

        /**
         * 时间查询条件规则
         * 查询条件开始时间小于数据库开始时间字段，或查询条件结束时间大于数据库结束时间字段
         */
        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)) {
            String dateQuerySql = "((UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(START_DATE) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(START_DATE)) " +
                    "or " +
                    "(UNIX_TIMESTAMP('" + startDate + " 00:00:00') <= UNIX_TIMESTAMP(END_DATE) " +
                    "and UNIX_TIMESTAMP('" + endDate + " 23:59:59') >= UNIX_TIMESTAMP(END_DATE)))";
            queryWrapper.apply(dateQuerySql);
        }

        queryWrapper.orderByDesc("LAST_UPDATE_DATE");

        IPage<LeaveRecord> LeaveRecordList = leaveRecordMapper.selectPage(page, queryWrapper);

        return LeaveRecordList;
    }

    public LeaveRecord getLeaveRecord(Map condition) throws Throwable {
        String leaveCode = condition.get("leaveCode") != null ? condition.get("leaveCode").toString() : "";
        String leaveId = condition.get("leaveId") != null ? condition.get("leaveId").toString() : "";
        String processInstID = condition.get("processInstID") != null ? condition.get("processInstID").toString() : "";

        QueryWrapper wrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(leaveCode)) {
            wrapper.eq("LEAVE_CODE", leaveCode);
        }

        if (StringUtils.isNotBlank(leaveId)) {
            wrapper.eq("LEAVE_ID", leaveId);
        }

        if (StringUtils.isNotBlank(processInstID)) {
            wrapper.eq("INS_CODE", processInstID);
        }

        wrapper.last("limit 1");

        LeaveRecord leaveRecord = this.leaveRecordMapper.selectOne(wrapper);
        return leaveRecord;
    }

    public void insertLeaveRecord(LeaveRecord record) {
        leaveRecordMapper.insert(record);
    }

    public void updateLeaveRecord(LeaveRecord record) {
        leaveRecordMapper.updateById(record);
    }

    /**
     * @param beginDateStr :
     * @param endDateStr   :
     * @param empCode      :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/7/20 2020/7/20
     */
    public void getLeaveDayDetail(String beginDateStr, String endDateStr, String empCode) {

    }

}
